﻿/*
Copyright (C) 2009  Tomasz Chrzanowski

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

using System;
using Com.TomCdc.ChangeDataCapture;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CdcSchemaInstaller.Tests
{
    /// <summary>
    /// Summary description for SourceColumnTests
    /// </summary>
    [TestClass]
    public class SourceColumnTests
    {
        public SourceColumnTests()
        {
        }

        private TestContext testContextInstance;

        private SourceColumn srcCol;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [TestInitialize]
        public void TestInitialize()
        {
            srcCol = new SourceColumn("abc", "int", 1);
        }

        [TestMethod]
        public void ConstructorNameTest()
        {
            SourceColumn testcol = new SourceColumn("abc", "int", 1);
            Assert.AreEqual("abc", testcol.ColumnName);
        }

        [TestMethod]
        public void ConstructorPositionTest()
        {
            SourceColumn testcol = new SourceColumn("abc", "int", 1);
            Assert.AreEqual(1, testcol.OrdinalPosition);
        }

        [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void ConstructorInvalidPositionTest()
        {
            SourceColumn testcol = new SourceColumn("abc", "int", 0);
        }

        [TestMethod]
        public void ActivationTest()
        {
            SourceColumn testcol = new SourceColumn("abc", "int", 1);
            testcol.IsTracked = true;
            Assert.AreEqual(true, testcol.ActivateTracking);
        }

        [TestMethod]
        public void Activation2Test()
        {
            SourceColumn testcol = new SourceColumn("abc", "int", 1);
            testcol.IsTracked = false;
            Assert.AreEqual(false, testcol.ActivateTracking);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void ConstructorEmptyNameTest()
        {
            SourceColumn testcol = new SourceColumn("", "int", 1);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void ConstructorNullNameTest()
        {
            SourceColumn testcol = new SourceColumn(null, "int", 1);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void ConstructorEmptyTypeTest()
        {
            SourceColumn testcol = new SourceColumn("abc", "", 1);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void ConstructorNullTypeTest()
        {
            SourceColumn testcol = new SourceColumn("abc", null, 1);
        }


        [TestMethod]
        public void PrimKeyTest()
        {
            srcCol.IsPrimaryKey = true;
            Assert.AreEqual(true, srcCol.IsPrimaryKey);
        }

        [TestMethod]
        public void DataTypeTest()
        {
            Assert.AreEqual("int", srcCol.DataType);
        }

        [TestMethod]
        public void DataTypeNoTrackableTest()
        {
            foreach (string nontype in CommonHelper.NotTrackableTypes)
            {
                srcCol = new SourceColumn("abc", nontype, 1);
                Assert.AreEqual(true, srcCol.IsDisabled);
            }
        }

        [TestMethod]
        public void DataTypeNoTrackableCaseTest()
        {
            if (CommonHelper.NotTrackableTypes.Count > 0)
            {
                srcCol = new SourceColumn("abc", CommonHelper.NotTrackableTypes[0].ToUpper(),1 );
                Assert.AreEqual(true, srcCol.IsDisabled);

                srcCol = new SourceColumn("abc", CommonHelper.NotTrackableTypes[0].ToLower(), 1);
                Assert.AreEqual(true, srcCol.IsDisabled);
            }
        }

        [TestMethod]
        public void MaxLenGraterZeroTest()
        {
            srcCol.MaxLength = 10;
            Assert.AreEqual(10, srcCol.MaxLength);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void MaxLenNegativeTest()
        {
            srcCol.MaxLength = -1;
        }

        [TestMethod]
        public void ActivateTrackingTest()
        {
            srcCol.ActivateTracking = true;
            Assert.AreEqual(true, srcCol.ActivateTracking);

            srcCol.ActivateTracking = false;
            Assert.AreEqual(false, srcCol.ActivateTracking);
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentException))]
        public void ActivateTrackingDisableColumnTest()
        {
            if (CommonHelper.NotTrackableTypes.Count > 0)
            {
                srcCol = new SourceColumn("abc", CommonHelper.NotTrackableTypes[0], 1);
                srcCol.ActivateTracking = true;
            }
        }

        [TestMethod]
        public void ActivateTrackingDisableColumnTest2()
        {
            if (CommonHelper.NotTrackableTypes.Count > 0)
            {
                srcCol = new SourceColumn("abc", CommonHelper.NotTrackableTypes[0], 1);
                srcCol.ActivateTracking = false;
                Assert.AreEqual(false, srcCol.ActivateTracking);
            }
        }

    }
}
